// 论坛相关API接口
import axios from 'axios'
import type { Category, Post, Comment, CreatePostData, CreateCommentData, PostPagination, CommentPagination } from '../types/forum'

// 创建axios实例
const api = axios.create({
  baseURL: '/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    console.error('API请求错误:', error)
    return Promise.reject(error)
  }
)

// 获取分类列表
export const getCategories = async (): Promise<Category[]> => {
  return await api.get('/forum/categories')
}

// 获取帖子列表
export const getPosts = async (params: {
  page?: number
  pageSize?: number
  categoryId?: number
  keyword?: string
  sortBy?: 'latest' | 'popular' | 'hot'
  status?: 'APPROVED' | 'PENDING' | 'REJECTED'
}): Promise<PostPagination> => {
  // 转换参数名以匹配后端API
  const apiParams = {
    page: params.page,
    size: params.pageSize,
    categoryId: params.categoryId,
    keyword: params.keyword,
    sortBy: params.sortBy,
    status: params.status
  }
  return await api.get('/forum/posts', { params: apiParams })
}

// 获取热门帖子
export const getHotPosts = async (count: number = 10): Promise<Post[]> => {
  return await api.get('/forum/posts/hot', { params: { count } })
}

// 获取最新帖子
export const getLatestPosts = async (count: number = 10): Promise<Post[]> => {
  return await api.get('/forum/posts/latest', { params: { count } })
}

// 获取帖子详情
export const getPostDetail = async (postId: number): Promise<Post> => {
  return await api.get(`/forum/posts/${postId}`)
}

// 获取帖子详情 (别名)
export const getPostById = getPostDetail

// 创建帖子
export const createPost = async (data: CreatePostData): Promise<Post> => {
  return await api.post('/forum/posts', data)
}

// 更新帖子
export const updatePost = async (postId: number, data: Partial<CreatePostData>): Promise<Post> => {
  return await api.put(`/forum/posts/${postId}`, data)
}

// 删除帖子
export const deletePost = async (postId: number): Promise<void> => {
  return await api.delete(`/forum/posts/${postId}`)
}

// 点赞/取消点赞帖子
export const likePost = async (postId: number): Promise<{ likeCount: number, isLiked: boolean }> => {
  return await api.post(`/forum/posts/${postId}/like`)
}

// 收藏/取消收藏帖子
export const collectPost = async (postId: number): Promise<{ isCollected: boolean }> => {
  return await api.post(`/forum/posts/${postId}/collect`)
}

// 获取帖子评论
export const getPostComments = async (postId: number, params: {
  page?: number
  pageSize?: number
  parentId?: number
}): Promise<CommentPagination> => {
  return await api.get(`/forum/posts/${postId}/comments`, { params })
}

// 创建评论
export const createComment = async (data: CreateCommentData): Promise<Comment> => {
  return await api.post('/forum/comments', data)
}

// 提交评论 (别名)
export const submitComment = async (postId: number, content: string): Promise<Comment> => {
  return await createComment({ postId, content })
}

// 删除评论
export const deleteComment = async (commentId: number): Promise<void> => {
  return await api.delete(`/forum/comments/${commentId}`)
}

// 点赞/取消点赞评论
export const likeComment = async (commentId: number): Promise<{ likeCount: number, isLiked: boolean }> => {
  return await api.post(`/forum/comments/${commentId}/like`)
}

// 管理员审核帖子
export const approvePost = async (postId: number, status: 'APPROVED' | 'REJECTED'): Promise<void> => {
  return await api.put(`/admin/forum/posts/${postId}/status`, { status })
}

// 管理员获取待审核帖子
export const getPendingPosts = async (params: {
  page?: number
  pageSize?: number
}): Promise<PostPagination> => {
  return await api.get('/admin/forum/posts/pending', { params })
}